package com.zz.comframe;

import treemap.zz.json.JSONArray;

public class Page {
	private int totalCount;
	private int ipage;
	//private List<List<Map<String, Object>>> result;
	//private List<List<Map<String, Object>>> gridheader;
	private JSONArray result;
	private JSONArray gridheader;

	//public List<List<Map<String, Object>>> getGridheader() {
	public JSONArray getGridheader() {
		return gridheader;
	}

	//public void setGridheader(List<List<Map<String, Object>>> gridheader) {
	public void setGridheader(JSONArray gridheader) {
		this.gridheader = gridheader;
	}

//	public Page(int totalCount, List result) {
	public Page(int totalCount, int p, JSONArray result) {
		this.totalCount = totalCount;
		this.result = result;
		this.ipage = p;
	}

//	public Page(int totalCount, List result, List gridheader) {
	public Page(int totalCount, int p, JSONArray result, JSONArray gridheader) {
		this.totalCount = totalCount;
		this.result = result;
		this.ipage = p;
		this.gridheader = gridheader;
	}

//	public Page(List result) {
	public Page(JSONArray result) {
		this.totalCount = result.length();
		// this.totalCount = totalCount;
		this.result = result;
	}

	public int getTotalCount() {
		return totalCount;
	}

//	public List getResult() {
	public JSONArray getResult() {
		return result;
	}
	public String PrintJSONString() {
		
		//return "{totalCount:" + totalCount + ",raws:" + result + "}";
		return "{\"curpage\":\""+this.ipage+"\",\"totalpages\":\""+0+"\",\"totalrecords\":\""+totalCount+"\",\"rows\":"+ result + "}";
	}

	public String getcolumModle() {
		String result = " ";
		/*String[] ls = { "UUID","align","firstsortorder","formoptions","formatoptions","formatter","hidden",
				"index","ckey","label","name","sorttype","stype","surl","width","cid" };
		for (int i = 0; i < gridheader.length(); i++) {
			result += "{";

			List<Map<String, Object>> l = gridheader.get(i);
			for (int k = 0; k < l.size(); k++) {
				Set<Map.Entry<String, Object>> mapset = l.get(k).entrySet();
				Map.Entry<String, Object> m = (Entry<String, Object>) mapset
						.toArray()[0];
				String str = m.getKey() != null ? m.getKey().toLowerCase()
						.trim() : "";
				//for (int n = 0; n < ls.length; n++) {					//使用jquerygrid，没有大小写问题
					if (m.getValue() != null && !("".equals(m.getValue().toString().trim()) )){//if ("" != str && str.equals(ls[n].toLowerCase())) { // grid里面特殊大小写，如dataIndex
						if (str.equals("cid"))
							result +=  "\"id\":\"" + m.getValue() + "\"," ;
						else if (str.equals("ckey"))
								result += "\"key\":" + m.getValue() + "," ;
						else {
							Object o = m.getValue();
							//if (o != null && o.toString().trim() != "")
								result += o.getClass().getName().toLowerCase()
										.indexOf("string")>=0&&"truefalse".indexOf(o.toString().toLowerCase())<0 ? "\"" + str
										+ "\":\"" + o + "\"," : "\""
										+ str + "\":" + o + ",";

							 result = result.replaceAll("TRUE", "true").replaceAll("FALSE", "false");
						}
					}
				//}
			}
			result = result.substring(0, result.length() - 1) + "},";
		}*/
		result = gridheader.toString();
		result = result.replaceAll("cid", "id").replaceAll("ckey", "key").replaceAll("TRUE", "true").replaceAll("FALSE", "false");
		return result;//.substring(0, result.length() - 1);
	}

	/*public String toDyExtJson() {
		String fields = "";
		String datajs = "[";
		String header = "[" + getcolumModle() + "]";

		// List<List<Map<String, String>>> list = this.getObjectList(sql);
		for (int i = 0; i < result.size(); i++) {
			fields = "[";
			datajs += "{";
			List<Map<String, Object>> l = result.get(i);
			for (int k = 0; k < l.size(); k++) {
				Set<Map.Entry<String, Object>> mapset = l.get(k).entrySet();
				Map.Entry<String, Object> m = (Entry<String, Object>) mapset
						.toArray()[0];
				Object o = m.getValue();
				if (o != null && o.toString() != ""){
					String s = o.getClass().getName().toLowerCase();
					//java.sql.Date,java.lang.String,java.lang.Integer
					datajs += (s.indexOf("string") >= 0 || s.indexOf("date") >= 0) ? "\"" + m.getKey() + "\":\""
					//datajs += (s.indexOf("Integer") >= 0 || s.indexOf("date") >= 0) ? "\"" + m.getKey() + "\":\""
							+ o + "\"," : "\"" + m.getKey() + "\":"
							+ o + ",";
				}
				fields += "{\"name\":\"" + m.getKey() + "\"},";
			}
			fields = fields.substring(0, fields.length() - 1) + "]";
			datajs = datajs.substring(0, datajs.length() - 1) + "},";
			datajs = datajs.replaceAll("TRUE", "true").replaceAll("FALSE", "false");
		}
		// fields = fields.substring(0, fields.length()-1);
		datajs = datajs.substring(0, datajs.length() - 1) + "]";
		return "{\"total\":" + this.totalCount + ",\"columns\":" + header
				+ ",\"rows\":" + datajs + ",\"fieldsNames\":" + fields + "}";
	}*/
}
